package com.dbq.fast.business.login.business;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.login.bean.CustomProfile;
import com.dbq.fast.business.login.bean.FbProfile;
import com.dbq.fast.business.login.bean.WeChatProfile;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.manager.DatabaseManager;
import com.dbq.fast.util.ApkUtils;
import com.dbq.fast.util.ProgressDialogUtil;
import com.facebook.AccessToken;
import com.facebook.appevents.AppEventsLogger;

import java.io.File;


public class LoginBiz implements WeChatLoginBiz.WeChatLoginListener, FbLoginBiz.FbLoginListener{

    public static final int LOGIN_TYPE_VISITOR = 0x01; //访客方式登录
    public static final int LOGIN_TYPE_WECHAT = 0x02; //微信方式登录
    public static final int LOGIN_TYPE_FB = 0x03; //Facebook方式登录
    public static final int LOGIN_TYPE_WB = 0x04; //Sina Weibo方式登录
    private AppEventsLogger logger;
    private Activity context;
    private LoginListener listener;
    private VisitorBiz visitorBiz;
    private WeChatLoginBiz weChatLoginBiz;
    private FbLoginBiz fbLoginBiz;

    public LoginBiz(Activity context){
        this.context = context;
        logger = AppEventsLogger.newLogger(context);
        accessParentInfo();
    }

    public void login(int type){
        switch (type){
            case LOGIN_TYPE_VISITOR:
                if(visitorBiz == null){
                    visitorBiz = new VisitorBiz();
                }
                CustomProfile profile = visitorBiz.generate();
                profile.setParentId(User.getInstance().getParentId());
                listener.loginSuccess(profile);
                break;
            case LOGIN_TYPE_WECHAT:
                if(weChatLoginBiz == null) {
                    weChatLoginBiz = new WeChatLoginBiz(context);
                }
                weChatLoginBiz.setLoginListener(this);
                weChatLoginBiz.login();
                break;
            case LOGIN_TYPE_FB:
                if(fbLoginBiz == null) {
                    fbLoginBiz = new FbLoginBiz(context);
                }
                fbLoginBiz.setLoginListener(this);
                fbLoginBiz.login();
                break;
        }
    }

    public void logOut(){
        if(fbLoginBiz == null){
            fbLoginBiz = new FbLoginBiz(context);
        }
        fbLoginBiz.logOut();
        DatabaseManager.getInstance().delAllProfile();
        User.getInstance().logOut();
    }

    public void destory(){
        if(weChatLoginBiz != null){
            weChatLoginBiz.destory();
            weChatLoginBiz = null;
        }
        if(fbLoginBiz != null){
            fbLoginBiz.destory();
            fbLoginBiz = null;
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(fbLoginBiz != null){
            fbLoginBiz.getCallbackManager().onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void weChatLoginSuccess(WeChatProfile weChatProfile) {
        Log.e("vpn_log", "微信登录成功，个人信息:"+weChatProfile.toString());
        YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVNET_WECHAT_LOGIN_SUCCESS);
        if (logger != null){
            logger.logEvent("");
        }
        if(weChatLoginBiz != null){
            CustomProfile profile = weChatLoginBiz.transProfile(weChatProfile);
            listener.loginSuccess(profile);
        }
    }

    @Override
    public void fbLoginSuccess(AccessToken accessToken) {
        Log.e("vpn_log", "FB登录成功");
        YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_FB_LOGIN_SUCCESS);
        ProgressDialogUtil.showProgress(context, "", App.getContext().getString(R.string.waiting)).show();
        if(fbLoginBiz != null) {
            fbLoginBiz.getProfile(accessToken);
        }
    }

    @Override
    public void fbAccessProfile(FbProfile fbProfile) {
        Log.e("vpn_log", "FB个人信息:"+fbProfile.toString());
        if(fbLoginBiz != null){
            CustomProfile profile = fbLoginBiz.transProfile(fbProfile);
            listener.loginSuccess(profile);
        }
    }

    public void setLoginListener(LoginListener listener){
        this.listener = listener;
    }

    public interface LoginListener{
        void loginSuccess(CustomProfile profile);
    }

    /**
     * 获取上家VpnId
     */
    private void accessParentInfo() {
        String path = ApkUtils.getPackagePath(App.getContext());
        if (path == null) return;
        String parentId = ApkUtils.readApk(new File(path));
        if(parentId == null) {
            Bundle bundle = new Bundle();
            bundle.putString(EventConstant.PARAM_INVITE_BY, "self");
            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_INSTALL_FROM, bundle);
            return;
        }else {
            Bundle bundle = new Bundle();
            bundle.putString(EventConstant.PARAM_INVITE_BY, parentId);
            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_INSTALL_FROM, bundle);

            User.getInstance().setParentId(parentId);
        }
        Log.e("yc_", "apk parentId:"+parentId);
        Log.e("yc_", "parentId:"+User.getInstance().getParentId());
    }
}
